package com.zara.store.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zara.store.common.EnvtHelper;
import com.zara.store.common.OfadHelper;
import com.zara.store.common.ProductVO;
import com.zara.store.common.StoreProxy;

public class StoreServlet extends HttpServlet {

	@Override
	public void init() throws ServletException {
		super.init();
		StoreProxy instance = StoreProxy.getInstance();
		try {
			instance.createOfad(new OfadHelper().createOfadFromXml(new File("C:/zara-workspace/zara.store.xml/ofad.xml")));
			instance.createEnvt(new EnvtHelper().createEnvtFromXml(new File("C:/zara-workspace/zara.store.xml/envt.xml")));
		} catch (IOException e) {
			throw new ServletException(e);
		}
	}
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		String barcode = req.getParameter("barcode");

		StoreProxy instance = StoreProxy.getInstance();
		ProductVO productVO = instance.queryProduct(barcode);
		if(productVO != null) {
			out.println(productVO);
		}
		out.flush();
	}
	
	private static final long serialVersionUID = 1L;

}
